import React, { useState, useEffect } from 'react';
import {
    Button,
    Cascader,
    DatePicker,
    Form,
    Input,
    InputNumber,
    Radio,
    Select,
    Switch,
    TreeSelect,
} from 'antd';
import zhCN from 'antd/locale/zh_CN';
import { ConfigProvider } from 'antd';
import type { FormProps } from 'antd';
import { async_addlist, async_updatelist } from '@/store/crmsSlice';
import { useAppDispatch, useAppSelector } from '@/store/reactreduxts';


const Myfrom: React.FC<any> = (props) => {

    let dispatch = useAppDispatch()

    //数据回显
    const [form] = Form.useForm();

    useEffect(() => {
        form.setFieldsValue(props.itemobj)
    }, [])


    const onFinish: FormProps<any>['onFinish'] = (values) => {
        if (props.title == '添加') {
            //执行添加的方法
            let obj = {
                ...values,
                id: new Date().getTime(),
                time: new Date().getTime(),
            }
            //执行添加的方法
            dispatch(async_addlist(obj))
        } else {
            //执行编辑的方法
            let obj = {
                ...values,
                id: props.itemobj.id,
                time: props.itemobj.time
            }
            dispatch(async_updatelist(obj))
        }
        //添加完成弹框消失
        props.handleOk()
    };

    return (
        <ConfigProvider locale={zhCN}>
            <Form onFinish={onFinish} form={form}>

                <Form.Item label="岗位编号" name='bianhao'
                    rules={[{ required: true, message: 'Please input your username!' }]}>
                    <Input />
                </Form.Item>

                <Form.Item label="岗位编码" name='bianma'
                    rules={[{ required: true, message: 'Please input your username!' }]}>
                    <Input />
                </Form.Item>

                <Form.Item label="岗位名称" name='title'
                    rules={[{ required: true, message: 'Please input your username!' }]}>
                    <Input />
                </Form.Item>

                <Form.Item label="岗位排序" name='paixu'
                    rules={[{ required: true, message: 'Please input your username!' }]}>
                    <Input />
                </Form.Item>

                <Form.Item label="岗位状态" name='zhuangt'>
                    <Radio.Group
                        options={[
                            { value: true, label: '正常' },
                            { value: false, label: '异常' },
                        ]}
                    />
                </Form.Item>

                <Form.Item>
                    <Button type='primary' htmlType="submit">完成</Button>
                </Form.Item>
            </Form>
        </ConfigProvider>
    );
};

export default Myfrom;